﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using loginSQL.SQLHelper;
using loginSQL.PrinterHelper;
using System.Drawing.Printing;
using loginSQL.Window.DataViewDetail;

namespace loginSQL.DataView
{
    public partial class searchResult : Form
    {
        delegate void showSearchResultDelegate();
        string query;
        int filter;
        
        
        List<List<string>> result;
        public searchResult(string query,int filter)
        {
            InitializeComponent();
            this.query = query;
            lbl_pencarian.Text = "Menunjukkan Hasil Pencarian Untuk : " + query;
            this.filter = filter;
            this.panel_loading.BringToFront();
            this.panel_loading.Dock = DockStyle.Fill;            
            bgworker.RunWorkerAsync();
        }

        public void addData(List<string> data)
        {
            Color backColor = Color.White;
            int count = dataGridView1.Rows.Count;
            DataGridViewRow row = new DataGridViewRow();  
            
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            if (count % 2 == 0)
                backColor = Color.GreenYellow;            
            style.BackColor = backColor; // the color change           
            row.DefaultCellStyle = style;
            row.Height = 400;
            row.MinimumHeight = 30;     
            row.CreateCells(dataGridView1, data.ToArray());
            dataGridView1.Rows.Add(row);
            //dataGridView1.Rows.InsertRange(0, row);
            
        }

        private void showResult()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new showSearchResultDelegate(showResult));
                return;
            }
            foreach (List<string> list in result)
            {
                addData(list);
            }
            panel_datagrid.BringToFront();
            panel_datagrid.Dock = DockStyle.Fill;
        }

        private void bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            DBHelper dbHelper = new DBHelper();
            result =  dbHelper.getSearchQuery(query, filter);           
            showResult();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /*if (e.ColumnIndex == dataGridView1.Columns.Count - 2)
            {               
                Preview preview = new Preview((string)dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.Columns.Count - 3].Value);
                preview.Show();
            } */
            ViewDetail viewDetailWindow = new ViewDetail(((string)dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.Columns.Count - 1].Value));
            viewDetailWindow.Show();
        }

        private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            
        }
    }
}
